#pragma strict

var explosionSound : AudioClip;
var driveSound : AudioClip;

private var mainCharacter : GameObject;
private var tankList : GameObject[];
private var shootProjectile : GameObject;

private var logoBZ3 : Component;

//Player values
private var pName : String;
private var score : int;
private var damage : int;
private var lifes : int;
private var audioVolume = 1;
private var driveAudioVolume = 1.0;
private var hitAudioSource : AudioSource;
private var driveAudioSource : AudioSource;

private var rotationToggle : boolean = true;
private var demo : boolean = true;
private var lastPosition : Vector3;

//Awake is before Start
function Awake(){
	hitAudioSource = gameObject.AddComponent(AudioSource);
	hitAudioSource.clip = explosionSound;
	hitAudioSource.loop = false;
	hitAudioSource.volume = audioVolume;
	hitAudioSource.playOnAwake = false; //Abspielen wenn es initialisiert wird
	
	driveAudioSource = gameObject.AddComponent(AudioSource);
	driveAudioSource.clip = driveSound;
	driveAudioSource.loop = true;
	driveAudioSource.volume = driveAudioVolume;
	driveAudioSource.playOnAwake = false;
}

function Start(){
	shootProjectile = GameObject.FindGameObjectWithTag("ProjectileSpawn");
	mainCharacter = GameObject.FindGameObjectWithTag("MainCharacter");
	logoBZ3 = GameObject.Find("battlezone_logo").GetComponent("GUITexture");
	lastPosition = transform.position;
}

function Update () {
	//FIXME TODO outsource into game controller (demo) [was a quicky, to get it quick done]
	if(Input.anyKeyDown){demo = false;}
	logoBZ3.active = false;
	
	if(demo){
		logoBZ3.active = true;
		tankList = GameObject.FindGameObjectsWithTag("Tank");
		
		if(tankList.length >= 1){
			for(var i=0; i<tankList.length; i++){
				var target = tankList[i];
				var targetPos = target.transform.position;
				var angleToTarget = Vector3.Angle(targetPos - mainCharacter.transform.position, mainCharacter.transform.forward);
				var distanceToTarget = Vector3.Distance(targetPos, mainCharacter.transform.position);
				
				if(distanceToTarget <= 150){
					mainCharacter.transform.LookAt(targetPos);
					shootProjectile.GetComponent(ShootProjectile).Shoot();
				}
			}
			 //TODO a nicer version (position) setPlayerScore  setPlayerLifes setPlayerDamage (inside of player!?!)
			setPlayerScore(0);
			setPlayerLifes(3);
			setPlayerDamage(0); //TODO increase damage of enemy tanks with level.
			
			// we rotate in the range of [-0.5:0.5]
			mainCharacter.transform.Rotate(Vector3.up * (Time.deltaTime * ((rotationToggle) ? 10 : -10)));
			if(mainCharacter.transform.rotation.y >= 0.5 || mainCharacter.transform.rotation.y <= -0.5){ rotationToggle = !rotationToggle; }	
		}
	}
	
	// Input.GetKey     true on every update
	// Input.GetKeyDown true once when pressed down
	if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S)) {
		driveAudioSource.volume = 1.0;
		driveAudioSource.Play();
	}
	if(!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) {
		driveAudioSource.volume -= Time.deltaTime *0.5;
	}
	
}


function OnCollisionEnter (collision : Collision) {
	
	if(collision.gameObject.tag == "Projectile"){
		
		//Player damage and life handling
		increasePlayerDamage(50);
		//Debug.Log("Player was hit.");
		if(damage>100){
			decreasePlayerLife(1);
			setPlayerDamage(0);
			if(lifes<=0){
				//TODO PlayerTank Destroy Animation
				//TODO PlayerTank Destroy Sound
			}
		}
		hitAudioSource.Play();
	}
}

public function getPlayerName() : String{
	return pName;
}

public function setPlayerName(value : String){
	pName = value;
}

public function getPlayerScore() : int{
	return score;
}

public function setPlayerScore(value : int){
	score = value;
}

public function increasePlayerScore(value : int){
	score += value;
}

public function getPlayerDamage() : int{
	return damage;
}

public function setPlayerDamage(value : int){
	damage = value;
}

public function increasePlayerDamage(value : int){
	damage += value;
}

public function getPlayerLifes() : int{
	return lifes;
}

public function setPlayerLifes(value : int){
	lifes = value;
}

public function decreasePlayerLife(value : int){
	lifes -= value;
}

public function increasePlayerLife(value : int){
	lifes += value;
}